package com.kjlink.internal.workreport.helper;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.WeekFields;
import java.util.Date;
import java.util.Locale;

/**
 * 描述:
 * 周计划时间的一些帮助类
 *
 * @author Vic.xu
 * @since 2024-10-11 14:15
 */
public class WeekDateHelper {

    /**
     * 获取一个日期所在周次
     * 周一到周日
     *
     * @return yyyy-周次
     */
    public static String weekNumber(Date date) {

        // 将 java.util.Date 转换为 java.time.LocalDate
        LocalDate localDate = date.toInstant()
                .atZone(ZoneId.systemDefault())
                .toLocalDate();

        // 获取当前日期是本年度的第几周
        // 使用以周一为每周起始日的 WeekFields
        WeekFields weekFields = WeekFields.of(DayOfWeek.MONDAY, 1);
        int weekOfYear = localDate.get(weekFields.weekOfYear());

        // 获取年份
        int year = localDate.getYear();

        // 拼接成 "yyyy-周次" 格式
        String result = year + "-" + weekOfYear;

        return result;
    }

    public static void main(String[] args) {
        WeekFields weekFields = WeekFields.of(Locale.getDefault());
        // 获取一周的第一天是星期几
        System.out.println("一周的第一天是: " + weekFields.getFirstDayOfWeek());
        weekNumber(new Date());
    }
}
